//
//  ChooseAddressViewController.m
//  XDBaseFrameWork
//
//  Created by XD-XY on 9/24/14.
//  Copyright (c) 2014 XD-XY. All rights reserved.
//

#import "ChooseAddressViewController.h"
#import "SearchAddressCell.h"

@interface ChooseAddressViewController ()

@end

@implementation ChooseAddressViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    poisearch.delegate = nil;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.titleLabel.text = @"当前位置";
    
    mDataArray = [[NSMutableArray alloc] init];
    
    poisearch = [[BMKPoiSearch alloc]init];
    poisearch.delegate = self;
    
    [self initViews];
}

#pragma mark
#pragma mark UI 布局
-(void)initViews
{
    UIImageView * inputIV = [UIImageView initImageViewRect:CGRectMake((640-582)/4.0f, 15, 582/2.0f, 41) andImage:LOADIMAGE(@"inputaddress_bg@2x") andUserInteractionEnabled:YES];
    [self.contentView addSubview:inputIV];
    
    addressTF = [[UITextField alloc] init];
    addressTF.frame = CGRectMake(37, 0, 250, 41);
    addressTF.backgroundColor = [UIColor clearColor];
    addressTF.returnKeyType = UIReturnKeyDone;
    addressTF.font = [UIFont systemFontOfSize:15.5];
    addressTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    addressTF.contentVerticalAlignment =  UIControlContentVerticalAlignmentCenter;
    addressTF.delegate = self;
    [addressTF addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    [inputIV addSubview:addressTF];
    [addressTF becomeFirstResponder];
    
    mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, height_y(inputIV), UI_SCREEN_WIDTH, self.contentView.frame.size.height-(height_y(inputIV))) style:UITableViewStylePlain];
    mTableView.backgroundColor = [UIColor clearColor];
    mTableView.dataSource = self;
    mTableView.delegate = self;
    mTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:mTableView];
}

#pragma mark
#pragma mark UITextField Delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)textChange:(UITextField *)textField
{
    if (IS_NOT_EMPTY(textField.text)){
        [self getDatas];
    }else{
        [mDataArray removeAllObjects];
        [mTableView reloadData];
    }
}

#pragma mark
#pragma mark UITableView Delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 88/2.0f;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [mDataArray count];

}

-(SearchAddressCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellName = @"cell";
    SearchAddressCell * cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (cell == nil){
        cell = [[SearchAddressCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    BMKPoiInfo * poi = [mDataArray objectAtIndex:indexPath.row];
    cell.titleLB.text = poi.name;
    cell.contentLB.text = poi.address;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BMKPoiInfo * poi = [mDataArray objectAtIndex:indexPath.row];
    [_delegate chooseBMKPoiInfoFromBaiDuMap:poi];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark
#pragma mark UIScrollView Delegate
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [addressTF resignFirstResponder];
}

#pragma mark
#pragma mark 重写返回方法
- (void)backPrePage
{
    [_delegate chooseBMKPoiInfoFromBaiDuMap:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)getDatas
{
    BMKCitySearchOption * citySearchOption = [[BMKCitySearchOption alloc]init];
    citySearchOption.pageIndex = 0;
    citySearchOption.pageCapacity = 50;
    citySearchOption.city= @"北京";
    citySearchOption.keyword = addressTF.text;
    BOOL flag = [poisearch poiSearchInCity:citySearchOption];
    if(flag){
        NSLog(@"城市内检索发送成功");
    }else{
        NSLog(@"检索失败");
    }

}

#pragma mark -
#pragma mark implement BMKSearchDelegate
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult*)result errorCode:(BMKSearchErrorCode)error
{
    if(IS_NOT_EMPTY(addressTF.text)){
        DDLOG(@"There are some words ! ! !");
    }else{
        DDLOG(@"There are no words ! ! !");
        [mDataArray removeAllObjects];
        [mTableView reloadData];
        return;
    }
    
    if (error == BMK_SEARCH_NO_ERROR){
        [mDataArray removeAllObjects];
        [mDataArray addObjectsFromArray:result.poiInfoList];
        [mTableView reloadData];
    }else if (error == BMK_SEARCH_RESULT_NOT_FOUND){
//        [XDTools showTips:@"没有找到检索结果" toView:self.view];
    }else{
//        [XDTools showTips:@"检索失败" toView:self.view];
    }

}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
